// *#5239870*#
// *#7092463*#

package GPSTracker;

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.media.*;
import java.util.*;
import java.io.*;
import sUtils.*;
//import javax.microedition.io.file.*;
//import javax.microedition.io.*;

/**
 *
 * @author thomas.s
 */
public class GPSTrackerMenu extends MenuView {
    Image bg;
    Display display;
    GPSTrackerView viewer;
    GPSTrackerMain main;
    GPSTrackerThread gpx;
    INIFile ini;
    
    public double digit_size;
    public int digit_mirror;
    public int digit_color;
    public int display_gps;
    public double speed_factor, dist_factor, alt_corr;
    public String speed_name, dist_name;
    public boolean newLanguage;
    public int lang;
    public String digit_file, bg_file;

    /*
    Slider sld_alt;
    Button btn_track, btn_reset;
    Button btn_speedkmh, btn_speedms, btn_speedkn, btn_speedmph;
    Button btn_white, btn_red, btn_green, btn_blue, btn_gray, btn_orange, btn_cyan, btn_magenta;
    Button btn_hud, btn_hudsize, btn_gps;
    Button btn_eng, btn_deu, btn_rus;

    Header header_tour, header_display, header_meas, header_lang, header_alt;
    */
    int y0=0,x0=0;

    public GPSTrackerMenu ( Display d, GPSTrackerThread g, GPSTrackerMain m ) {
        newLanguage = false;
        lang = 0;
        viewer = null;
        main = m;
        display = d;
        gpx = g;

        ini = new INIFile();
        ini.read("GPSTracker.ini");
        lang = ini.getIntValue("language", 0);
        display_gps = ini.getIntValue("display_gps", 1);
        digit_size = ini.getDoubleValue("digit_size", 100);
        digit_mirror = ini.getIntValue("digit_mirror", 1);
        digit_color = ini.getIntValue("digit_color", 0xffffff);
        dist_factor = ini.getDoubleValue("dist_factor", 0.001);
        dist_name = ini.getValue("dist_name", "km");
        speed_factor = ini.getDoubleValue("speed_factor", 3.6);
        speed_name = ini.getValue("speed_name", "km/h");
        alt_corr = ini.getDoubleValue("alt_corr", 41.0);
        bg_file = ini.getValue("bg_file", "");
        digit_file = ini.getValue("digit_file", "");


        addHeader( "Aufzeichnung","header_tour" );
        addButton( "Tour aufzeichnen","track",1);
        addLine(10);

        addHeader("Daten","header_meas");
        addButton("Distanz zurücksetzen","reset",1);

        addButton("km/h","speedkmh",0.25);
        addButton("m/s","speedms",0.25);
        addButton("kn","speedkn",0.25);
        addButton("mph","speedmph",0.25);
        addLine(10);

        addHeader("Höhenkorrektur","header_alt");
        addSlider("m","alt_corr",0,100,alt_corr);


        addHeader("Anzeige","header_display");
       
        addButton("W","white",0.125);
        addButton("R","red",0.125);
        addButton("G","green",0.125);
        addButton("B","blue",0.125);
        addButton("O","orange",0.125);
        addButton("C","cyan",0.125);
        addButton("M","magenta",0.125);
        addButton("D","gray",0.125);
        
        setColor("white",0xffffff);
        setColor("red",0xff4040);
        setColor("green",0x40ff40);
        setColor("blue",0x4040ff);
        setColor("orange",0xffa040);
        setColor("cyan",0x40ffff);
        setColor("magenta",0xff40ff);
        setColor("gray",0x808080);

        
        addButton("HUD","hud",0.33);
        addButton("groß","hudsize",0.33);
        addButton("GPS","gps",0.34);

        addHeader("Language/Sprache","header_lang");
        addButton("deutsch","deu",0.33);
        addButton("english","eng",0.33);
        addButton("русский","rus",0.34);

        updateButtonText();
        updateButtons();
    }

    public boolean langUpdated()
    {
        boolean updated = newLanguage; newLanguage=false;
        return updated;
    }
    public void setViewer( GPSTrackerView v ) { viewer=v; }
    public void setActive()
    {
        if (display!=null) {
             display.setCurrent(this);
             display.flashBacklight(50);
        }
    }

    public void close()
    {
        ini.setValue("language", new Integer(lang).toString());
        ini.setValue("digit_size", new Double(digit_size).toString());
        ini.setValue("digit_mirror", new Integer(digit_mirror).toString());
        ini.setValue("digit_color", new Integer(digit_color).toString());
        ini.setValue("speed_factor", new Double(speed_factor).toString());
        ini.setValue("speed_name", speed_name);
        ini.setValue("dist_factor", new Double(dist_factor).toString());
        ini.setValue("dist_name", dist_name);
        ini.setValue("display_gps", new Integer(display_gps).toString());
        ini.setValue("alt_corr", new Double(alt_corr).toString());
        ini.setValue("digit_file", digit_file);
        ini.setValue("bg_file", bg_file);

        ini.write("GPSTracker.ini");
    }


    public void onAction( String obj ) {
        if (obj==null) return;
        boolean updatedigits=false;
        //display.vibrate(20);
        
        if (obj.equals("track"))
        {
            if (gpx.recording()) gpx.stop(); else gpx.record();
        }
        if (obj.equals("reset")) gpx.reset();

        if (obj.equals("speedkmh")) { speed_factor = 3.6; speed_name = "km/h"; dist_factor = 0.001; dist_name = "km"; } else
        if (obj.equals("speedms")) { speed_factor = 1; speed_name = "m/s"; dist_factor = 1; dist_name = "m"; } else
        if (obj.equals("speedkn")){ speed_factor = 3600.0/1852.0; speed_name = "kn"; dist_factor = 1.0/1852.0; dist_name = "kn"; }
        if (obj.equals("speedmph")) { speed_factor = 3600.0/1609.0; speed_name = "mph"; dist_factor = 1.0/1609.0; dist_name = "miles"; }

        if (obj.equalsIgnoreCase("white")) { digit_color=0xffffff; updatedigits=true; }else
        if (obj.equalsIgnoreCase("red")) { digit_color=0xff4040; updatedigits=true; }else
        if (obj.equalsIgnoreCase("green")) { digit_color=0x40ff40; updatedigits=true; }else
        if (obj.equals("blue")) { digit_color=0x4040ff; updatedigits=true; }else
        if (obj.equals("orange")) { digit_color=0xffa040; updatedigits=true; }else
        if (obj.equals("cyan")) { digit_color=0x40ffff; updatedigits=true; }else
        if (obj.equals("magenta")) { digit_color=0xff40ff; updatedigits=true; }else ;
        if (obj.equals("gray")) { digit_color=0x808080; updatedigits=true; }else


        if (obj.equals("hud")) { digit_mirror=digit_mirror!=0?0:1; updatedigits=true; }
        if (obj.equals("hudsize")) { digit_size=digit_size<100?100:33; updatedigits=true; }
        if (obj.equals("gps")) { display_gps=(display_gps!=0?0:1); updatedigits=true; }

        if (obj.equals("deu")) { lang = 0; updateButtonText(); }
        if (obj.equals("eng")) { lang = 1; updateButtonText(); }
        if (obj.equals("rus")) { lang = 2; updateButtonText(); }
        
        if (obj.equals("alt_corr")) { alt_corr = getVal("alt_corr"); }
        
        if (updatedigits) viewer.updateDigits();
        updateButtons();

    }

    public void updateButtons()
    {
        setOn("track", gpx.recording());

        setOn("speedkmh",(dist_name.equals("km")));
        setOn("speedms",(dist_name.equals("m")));
        setOn("speedkn",(dist_name.equals("kn")));
        setOn("speedmph",(dist_name.equals("miles")));

        setOn("white",(digit_color&0xffffff)==0xffffff);
        setOn("red",(digit_color&0xffffff)==0xff4040);
        setOn("green",(digit_color&0xffffff)==0x40ff40);
        setOn("blue",(digit_color&0xffffff)==0x4040ff);
        setOn("gray",(digit_color&0xffffff)==0x808080);
        setOn("orange",(digit_color&0xffffff)==0xffa040);
        setOn("cyan",(digit_color&0xffffff)==0x40ffff);
        setOn("magenta",(digit_color&0xffffff)==0xff40ff);

        setOn("hud",(digit_mirror!=0));
        setOn("hudsize",(digit_size==100));
        setOn("gps",display_gps!=0);

        setOn("deu",lang==0);
        setOn("eng",lang==1);
        setOn("rus",lang==2);
        repaint();
    }

    public void updateButtonText()
    {
        newLanguage = true;
        setText("header_tour", getText("Recording") );
        setText("track", getText("Record tour"));

        setText("header_meas", getText("Measurement"));
        setText("reset", getText("Reset distance"));
        setText("speedkmh",getText("km/h"));
        setText("speedms",getText("m/s"));
        setText("speedkn",getText("kn"));
        setText("speedmph",getText("miles"));

        setText("header_alt",getText("Altitude correction"));

        setText("header_display",getText("Display"));
        setText("hud",getText("HUD"));
        setText("hudsize",getText("large"));

        setText("header_lang",getText("Language/Sprache/Язык"));
    }

    public String getText( String id )
    {
        if (lang==0) // deutsch
        {
            if (id.equals("Recording")) return "Aufzeichnung"; else
            if (id.equals("Record tour")) return "Tour aufzeichnen"; else
            if (id.equals("Measurement")) return "Daten"; else

            if (id.equals("Reset distance")) return "Strecke zurücksetzen"; else
            if (id.equals("Altitude correction")) return "Höhenkorrektur"; else
            if (id.equals("Display")) return "Anzeige"; else
            if (id.equals("large")) return "groß"; else
            if (id.equals("Language")) return "Sprache"; else
            if (id.equals("Exit")) return "Beenden"; else
            if (id.equals("Menu")) return "Menü"; else

            if (id.equals("File:")) return "Datei:"; else
            if (id.equals("time:")) return "Zeit:"; else
            if (id.equals("Dist:")) return "Strecke:"; else
            if (id.equals("Lat:")) return "Breite:"; else
            if (id.equals("Lon:")) return "Länge:"; else
            if (id.equals("Alt:")) return "Höhe:"; else
            if (id.equals("Mag:")) return "Kompass:"; else
            if (id.equals("Speed:")) return "Geschw.:"; else
            if (id.equals("v-max:")) return "v-max:"; else
            if (id.equals("v-avg:")) return "v-mittel:"; else
            if (id.equals("wait for signal...")) return "Warte auf Signal..."; else
            return id;
        } else
        if (lang==2) // russisch
        {

            if (id.equals("Recording")) return "Запись"; else
            if (id.equals("Record tour")) return "Записать путь"; else
            if (id.equals("Measurement")) return "Данные"; else

            if (id.equals("Reset distance")) return "Обнулить путь"; else
            if (id.equals("Altitude correction")) return "Kорекция высоты"; else
            if (id.equals("Display")) return "Стиль"; else
            if (id.equals("large")) return "крупнее"; else
            if (id.equals("Language")) return "Язык"; else
            if (id.equals("Measurement")) return "Daten"; else
            if (id.equals("Exit")) return "Выход"; else
            if (id.equals("Menu")) return "Меню"; else

            if (id.equals("File:")) return "Файл:"; else
            if (id.equals("time:")) return "время:"; else
            if (id.equals("Dist:")) return "Путь:"; else
            if (id.equals("Lat:")) return "Широта:"; else
            if (id.equals("Lon:")) return "Долгота:"; else
            if (id.equals("Alt:")) return "Высота:"; else
            if (id.equals("Mag:")) return "Компас:"; else
            if (id.equals("Speed:")) return "\u0421\u043A\u043E\u0440.:"; else
            if (id.equals("v-max:")) return "макс.:"; else
            if (id.equals("v-avg:")) return "сред.:"; else
            if (id.equals("wait for signal...")) return "поиск спутника..."; else
            return id;
        }
        return id;
    }



    public void keyPressed( int key )
    {
        if (key==-10) { if (viewer!=null) viewer.setActive(); }
        if (key==-1) { y0+=20; if (y0>0) y0=0; repaint(); }
        if (key==-2) { y0-=20; if (getHeight()-y0>700) y0=getHeight()-700; repaint(); }
        if (key==-3) { x0+=20; if (x0>0) x0=0; repaint(); }
        if (key==-4) { x0-=20; if (getWidth()-x0>480) x0=getWidth()-480; repaint(); }
    }

}
